{{ $images := resources.Match "images/bg/upload/*.{jpg,png,webp}" }}

{{ $result := dict
    "meta" (dict
        "generated_at" (now.Format "2006-01-02T15:04:05Z07:00")
        "count" (len $images)
    )
    "images" (slice)
}}

{{/*{{ partial "helpers/log" (dict "message" (printf "初始化信息: %v" ($result ))) }}*/}}

{{ with $images }}
    {{ range $img := $images }}
        {{ $absPath := printf "themes/WebStack-Hugo/assets%s" $img.Name }}
        {{ $config := imageConfig $absPath }}
        {{ $sizeInKB := div ($img.Content | len) 1024.0 | math.Round }}
        {{ $newImage := dict
            "name" (path.Base $img.Name)
            "url" $img.RelPermalink
            "size" $sizeInKB
            "width" $config.Width
            "height" $config.Height
        }}
{{/*        {{ partial "helpers/log" (dict "message" (printf "照片信息: %v" $newImage)) }}*/}}

        {{ $result = merge $result (dict
            "images" (append  $result.images (slice $newImage))
        ) }}
    {{ end }}
{{ else }}
    {{ $result = merge $result (dict
        "error" "未找到任何图片"
        "meta" (merge $result.meta (dict "count" 0))
    ) }}
{{ end }}

{{/*{{ partial "helpers/log" (dict "message" (printf "照片列表: %v" $result)) }}*/}}

{{/* 手动构建JSON保证顺序 */}}
{
    "meta": {
        "generated_at": "{{ $result.meta.generated_at }}",
        "count": {{ $result.meta.count }}
    },
    "images": [
        {{ range $i, $img := $result.images }}
            {{/* 正确处理逗号*/}}
            {{ if $i }},{{ end }}
            {
                "name": "{{ $img.name }}",
                "url": "{{ $img.url }}",
                "size": {{ $img.size }},
                "width": {{ $img.width }},
                "height": {{ $img.height }}
            }
        {{ end }}
    ]
}